package com.yantong.controller.platform;

import com.yantong.annotation.Log;
import com.yantong.controller.BaseController;
import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.MemberCard;
import com.yantong.service.CardService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 银行卡控制类
 * @Auther: kanghaixin
 * @Date: 2018/11/13 20:30
 * @Description:
 */
@Controller
@RequestMapping("/member/card")
public class CardController extends BaseController {
    private static final String prefix = "member/card";
    @Autowired
    private CardService cardService;

    /**
     * 银行卡列表页面
     * @param model
     * @return
     */
    @GetMapping()
    @RequiresPermissions("member:card:card")
    public String cards(Model model) {
        model.addAllAttributes(getCentreIdModelInfo());
        return prefix + "/cards";
    }

    /**
     * 银行卡列表
     * @param queryItem
     * @param centreId
     * @param mobile
     * @param offset
     * @param limit
     * @return
     */
    @ResponseBody
    @RequiresPermissions("member:card:card")
    @GetMapping("/list")
    public PageUtils<MemberCard> listMemberCards(String queryItem, Long centreId, String mobile, int offset, int limit) {
        if (centreId == null) {
            centreId = getUserCentreId();
        }
        return cardService.listMemberCards(queryItem, centreId, mobile, offset, limit);
    }

    /**
     * 会员银行卡详情
     * @param cdId
     * @param model
     * @return
     */
    @GetMapping("/detail/{cdId}")
    @RequiresPermissions("member:card:detail")
    public String cardDetail(@PathVariable Long cdId, Model model) {
        model.addAttribute("memberCard", cardService.getByKey(cdId));
        return prefix + "/detail";
    }

    /**
     * 删除绑定的银行卡记录
     * @param cdId
     * @return
     */
    @Log(module = "银行卡管理", operation = "删除绑定的银行卡记录")
    @ResponseBody
    @RequiresPermissions("member:card:remove")
    @PostMapping("/remove")
    public ResultVo remove(Long cdId) {
        if (cdId == null) {
            return ResultVo.error();
        }
        MemberCard mc = new MemberCard();
        mc.setIsDelete(1);
        mc.setCdId(cdId);
        if (cardService.updateCard(mc) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }
}
